package com.ruoyi.mining.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mining.mapper.StudentMapper;
import com.ruoyi.mining.domain.Student;
import com.ruoyi.mining.service.IStudentService;

/**
 * 学生信息Service业务层处理
 *
 * @author liyuxi
 * @date 2024-04-19
 */
@Service
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private StudentMapper studentMapper;


    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public Student selectStudentById(Long id) {
        return studentMapper.selectStudentById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student) {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public int insertStudent(Student student) {
        return studentMapper.insertStudent(student);
    }

    /**
     * 修改学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public Student updateStudent(Student student) {
        Student vo= studentMapper.queryStudentByIdOrPhone(student.getPhone());


        student.setCreateDate(DateUtils.getCurrentTime());
        int updateResult = 0;
        if (vo == null ||  vo.getId() == null) {//        数据库里面不存在 就插入

            updateResult = this.insertStudent(student);
        } else {
            updateResult =studentMapper.updateStudent(student);
        }
//        修改学生信息的时候 ，要把用户信息一起也改了
        SysUser user= studentMapper.querySysUserById(student.getUserId());
        if(user!=null && StringUtils.isEmpty( user.getPhonenumber() ) ){
            user.setUserName(student.getName());
            user.setNickName(student.getName() );
            user.setPhonenumber( student.getPhone());
            user.setIdCard( student.getIdCard());
            user.setCreateBy( "小程序");
            studentMapper.updateSysUser(user);
        }
        vo= studentMapper.queryStudentByIdOrPhone(student.getPhone());

        return vo;
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(Long[] ids) {
        return studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentById(Long id) {
        return studentMapper.deleteStudentById(id);
    }

    @Override
    public Student queryStudentByIdOrPhone(String userId) {
        Student student = studentMapper.queryStudentByIdOrPhone(userId);
        if(student ==null){
            studentMapper.syncSysUser(userId);
        }
        student = studentMapper.queryStudentByIdOrPhone(userId);
        return student;
    }
}
